package service;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ChatServer {
    public static void main(String[] args) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器启动成功");
            // 等待客户端连接
            while (true) {
                Socket socket = serverSocket.accept();//开始监听
                OutputStream out = socket.getOutputStream();
                //获取ip
                String ip = socket.getInetAddress().getHostAddress();
                //获取端口号
                String port = String.valueOf(socket.getPort());
                System.out.println("用户" + ip + ":" + port + "已连接");
                new Thread(new ChatThread(socket, ip, port)).start();
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}